package edu.jmu.service.impl;

import edu.jmu.dao.PictureDao;
import edu.jmu.domain.Picture;
import edu.jmu.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureDao pictureDao;

    @Transactional
    @Override
    public void deletePicture(List<Integer>list) {
        for (int i: list) {
            List<Picture> pictureList = pictureDao.findByNewsId(i);
            for (Picture p : pictureList) {
                String path = p.getPicturePath();
                System.out.println(path);
                File file = new File(path);
                if (file.exists()) {
                    file.delete();
                }
            }
            pictureDao.deletePicture(i);
        }
    }

    @Transactional
    @Override
    public void savePicture(String path,int newsId) {
        Picture picture = new Picture();
        picture.setNewsId(newsId);
        picture.setPicturePath(path);
        pictureDao.savePicture(picture);
    }


}
